package com.ymatou.iapi.sellerproduct.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.SetRecommendCall;
import com.ymatou.iapi.sellerproduct.service.SetTopMoveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.LiveProductInfoWapper;
import com.ymttest.database.mongo.LiveProductModelWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.mongo.jproduct.LiveProductsWapper;
import com.ymttest.database.sqlwapper.YmtProductsInLiveWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_SetTopMove {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static YmtProductsInLiveWapper ymtProductsInLiveWapper = new  YmtProductsInLiveWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("App-直播置顶商品移动");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new SetTopMoveCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SetTopMove() {
		Logger.start(true, "App-直播置顶商品移动");
		try{   
			//1 创建直播
			JSONObject liveResponseBean = LiveCallService.createLiveInProgress(sellerName, sellerId);
			int liveId =  liveResponseBean.getJSONObject("Data").getInt("ActivityId");
			
			//2 发布2个商品
			JSONObject productReqObject1 = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null);
			String productId1 =  productReqObject1.getJSONObject("Data").getString("ProductID");
			
			JSONObject productReqObject2 = SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null);
			String productId2 =  productReqObject2.getJSONObject("Data").getString("ProductID");
			
			
			//3 add product to live
	    	SellerProductCallService.addProductInLive(sellerId,liveId,productId1);
	        SellerProductCallService.addProductInLive(sellerId,liveId,productId2);
	        
	        
	        //4 set recommend
	        SetRecommendCall setrecommend = new SetRecommendCall();
	        
			Map<String,Object> updateMap = new HashMap<String, Object>();
			updateMap.put("UserId", sellerId);
			updateMap.put("ActivityId", liveId);
			updateMap.put("ProductId", productId1);
			JSONObject recommendBean = setrecommend.callAndGetReturnData(updateMap);
			Logger.verifyEquals(true, recommendBean.getJSONObject("Data").getBoolean("Success"), "响应报文-Success");
			
			updateMap.put("ProductId", productId2);
			recommendBean = setrecommend.callAndGetReturnData(updateMap);
			Logger.verifyEquals(true, recommendBean.getJSONObject("Data").getBoolean("Success"), "响应报文-Success");
	        
	        
	        //4 set top move
			JSONObject setTopMoveBean = new SetTopMoveCall().callAndGetReturnData(sellerId, liveId,productId1,productId2);
			
			Logger.verifyEquals(200, setTopMoveBean.getInt("Code"), "响应报文-返回码");
			Logger.verifyEquals(1, setTopMoveBean.getJSONObject("Data").getInt("_status"), "响应报文-返回状态");
			Logger.verifyEquals(1, setTopMoveBean.getJSONObject("Data").getInt("Status"), "响应报文-返回状态");
			
			
			//check SQL-Ymt_ProductsInLive
			Map<String, Object> YmtProductsInLiveMap1 = new YmtProductsInLiveWapper().selectInLiveByProductId(productId1).get(0);
			Map<String, Object> YmtProductsInLiveMap2 = new YmtProductsInLiveWapper().selectInLiveByProductId(productId2).get(0);
			Logger.verifyEquals(1, Integer.parseInt(YmtProductsInLiveMap1.get("iIsTop").toString()), "SQL-Ymt_ProductsInLive isTop");
			Logger.verifyEquals(1, Integer.parseInt(YmtProductsInLiveMap2.get("iIsTop").toString()), "SQL-Ymt_ProductsInLive isTop");
			
			Float Sort1 = Float.parseFloat(YmtProductsInLiveMap1.get("dReadyPutawayProductSort").toString());
			Float Sort2 = Float.parseFloat(YmtProductsInLiveMap2.get("dReadyPutawayProductSort").toString());
			Logger.verifyEquals(true, (Sort2 - Sort1)>0, "SQL-Ymt_ProductsInLive dReadyPutawayProductSort");
			
			
			
			//check mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object> liveProductInfoMap1 = new LiveProductInfoWapper().getliveproductByProductId(productId1);
				Map<String, Object> liveProductInfoMap2 = new LiveProductInfoWapper().getliveproductByProductId(productId2);
				Logger.verifyEquals(true, Boolean.parseBoolean(liveProductInfoMap1.get("IsTop").toString()), "Mongo-LiveProductInfo isTop");
				Logger.verifyEquals(true, Boolean.parseBoolean(liveProductInfoMap2.get("IsTop").toString()), "Mongo-LiveProductInfo isTop");
				
				//product1 排名靠前 ，所以sort值更小
				long Product1Sort = Long.parseLong(liveProductInfoMap1.get("ReadyPutawayProductSort").toString());
				long Product2Sort = Long.parseLong(liveProductInfoMap2.get("ReadyPutawayProductSort").toString());
				boolean ret = (Product2Sort - Product1Sort)>0;
				Logger.verifyEquals(true, ret, "Mongo-LiveProductInfo ReadyPutawayProductSort");
			}
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				Map<String, Object> liveProductModelMap1 = new LiveProductModelWapper().getByProductId(productId1);
				Map<String, Object> liveProductModelMap2 = new LiveProductModelWapper().getByProductId(productId2);
				
				Logger.verifyEquals(true, Boolean.parseBoolean(liveProductModelMap1.get("IsTop").toString()), "Mongo-LiveProductModel isTop");
				Logger.verifyEquals(true, Boolean.parseBoolean(liveProductModelMap2.get("IsTop").toString()), "Mongo-LiveProductModel isTop");
				
				//product1 排名靠前 ，所以sort值更小
				long Product1Sort = Long.parseLong(liveProductModelMap1.get("ReadyPutawayProductSort").toString());
				long Product2Sort = Long.parseLong(liveProductModelMap2.get("ReadyPutawayProductSort").toString());
				boolean ret = (Product2Sort - Product1Sort)>0;
				Logger.verifyEquals(true, ret, "Mongo-LiveProductModel ReadyPutawayProductSort");
				
				
				
			}
			
			
		    if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
	        	//LiveProductModel
	        	//new BigDecimal(liveProductMap.get("sort").toString()).toPlainString()
		    	Map<String, Object> liveProductModelMap1 = new LiveProductsWapper().getByProductId(productId1);
				Map<String, Object> liveProductModelMap2 = new LiveProductsWapper().getByProductId(productId2);
				
				
				Logger.verifyEquals(true, Boolean.parseBoolean(liveProductModelMap1.get("istop").toString()), "Mongo-LiveProducts isTop");
				Logger.verifyEquals(true, Boolean.parseBoolean(liveProductModelMap2.get("istop").toString()), "Mongo-LiveProducts isTop");
				
		        int Product1_sortId_SQL_After = (int)Float.parseFloat(ymtProductsInLiveWapper.selectInLiveByProductId(productId1).get(0).get("dReadyPutawayProductSort").toString());
		        int Product2_sortId_SQL_After = (int)Float.parseFloat(ymtProductsInLiveWapper.selectInLiveByProductId(productId2).get(0).get("dReadyPutawayProductSort").toString());
		
		        Map<String, Object> prod1SortMgo = MapUtil.hashMap("sort", new BigDecimal(liveProductModelMap1.get("sort").toString()).toPlainString());
	        	Map<String, Object> prod2SortMgo = MapUtil.hashMap("sort", new BigDecimal(liveProductModelMap2.get("sort").toString()).toPlainString());
	        	
		    	AssertService.assertResultEqual(MapUtil.hashMap("sort", Product1_sortId_SQL_After), prod1SortMgo,"Mongo-LiveProducts sort1");
	        	AssertService.assertResultEqual(MapUtil.hashMap("sort", Product2_sortId_SQL_After), prod2SortMgo,"Mongo-LiveProducts sort");
	      
	        }
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}
